<?php
/**
 * Copyright (C) Alibaba Cloud Computing
 * All rights reserved
 */

require_once realpath(dirname(__FILE__) . '/../Log_Autoload.php');

function putLogs(Aliyun_Log_Client $client, $project, $logstore) {
    $topic = 'Fitdays';
    
    $contents = array( // key-value pair
        'request_time'=>'2020-12-24 01:08:45',
        'request_route'=>'Req-Middleware: /api/gateway/skips/uploadskipdata?app_ver=1.2.1&client_id=0A35777A3E5BA4C0CD8437FBEE11BC30&country=US&device_model=iPhone13,4&language=en&os_type=1&request_id=028F57467F4B769F86476007A0BA717B&source=2&sys_ver=IOS14.10&timestamp=1608772125&token=6c79975dfecc38d76c3a9f2547038867&uid=01d8494b33821000&sign=fc34d90e7d0cb27145529787d0d5f462',
        'exec_time'=>'0.0067040920257568',
        'request_data'=>'{"datas":[{"avg_freq":126,"skip_count":527,"measured_time":1608771629,"fat_burn_efficiency":648,"freqs":"[\n  {\n    \"duration\" : 56,\n    \"skip_count\" : 115\n  },\n  {\n    \"duration\" : 4,\n    \"skip_count\" : 5\n  },\n  {\n    \"duration\" : 7,\n    \"skip_count\" : 14\n  },\n  {\n    \"duration\" : 2,\n    \"skip_count\" : 3\n  },\n  {\n    \"duration\" : 12,\n    \"skip_count\" : 25\n  },\n  {\n    \"duration\" : 8,\n    \"skip_count\" : 15\n  },\n  {\n    \"duration\" : 5,\n    \"skip_count\" : 11\n  },\n  {\n    \"duration\" : 28,\n    \"skip_count\" : 65\n  },\n  {\n    \"duration\" : 10,\n    \"skip_count\" : 22\n  },\n  {\n    \"duration\" : 4,\n    \"skip_count\" : 6\n  },\n  {\n    \"duration\" : 26,\n    \"skip_count\" : 57\n  },\n  {\n    \"duration\" : 6,\n    \"skip_count\" : 10\n  },\n  {\n    \"duration\" : 4,\n    \"skip_count\" : 5\n  },\n  {\n    \"duration\" : 38,\n    \"skip_count\" : 86\n  },\n  {\n    \"duration\" : 2,\n    \"skip_count\" : 1\n  },\n  {\n    \"duration\" : 31,\n    \"skip_count\" : 73\n  },\n  {\n    \"duration\" : 7,\n    \"skip_count\" : 14\n  }\n]","sex":1,"fastest_freq":141,"data_id":"a852e38b16672603578608b1fb4b251e","suid":"01d84967a1821000","setting":0,"age":29,"weight":55,"mode":0,"calories_burned":45,"height":160,"device_id":"01d8549c68021000","elapsed_time":250}]}',
    );
    $logItem = new Aliyun_Log_Models_LogItem();
    $logItem->setTime(time());
    $logItem->setContents($contents);
    $logitems = array($logItem);
    $request = new Aliyun_Log_Models_PutLogsRequest($project, $logstore, $topic, null, $logitems);
    
    try {
        $response = $client->putLogs($request);
        var_dump($response);
    } catch (Aliyun_Log_Exception $ex) {
        var_dump($ex);
    } catch (Exception $ex) {
        var_dump($ex);
    }
}

function listLogstores(Aliyun_Log_Client $client, $project) {
    try{
        $request = new Aliyun_Log_Models_ListLogstoresRequest($project);
        $response = $client->listLogstores($request);
        var_dump($response);
    } catch (Aliyun_Log_Exception $ex) {
        var_dump($ex);
    } catch (Exception $ex) {
        var_dump($ex);
    }
}


function listTopics(Aliyun_Log_Client $client, $project, $logstore) {
    $request = new Aliyun_Log_Models_ListTopicsRequest($project, $logstore);
    
    try {
        $response = $client->listTopics($request);
        var_dump($response);
    } catch (Aliyun_Log_Exception $ex) {
        var_dump($ex);
    } catch (Exception $ex) {
        var_dump($ex);
    }
}

function getLogs(Aliyun_Log_Client $client, $project, $logstore) {
    $topic = 'TestTopic';
    $from = time()-3600;
    $to = time();
    $request = new Aliyun_Log_Models_GetLogsRequest($project, $logstore, $from, $to, $topic, '', 100, 0, False);
    
    try {
        $response = $client->getLogs($request);
        foreach($response -> getLogs() as $log)
        {
            print $log -> getTime()."\t";
            foreach($log -> getContents() as $key => $value){
                print $key.":".$value."\t";
            }
            print "\n";
        }

    } catch (Aliyun_Log_Exception $ex) {
        var_dump($ex);
    } catch (Exception $ex) {
        var_dump($ex);
    }
}

function getHistograms(Aliyun_Log_Client $client, $project, $logstore) {
    $topic = 'TestTopic';
    $from = time()-3600;
    $to = time();
    $request = new Aliyun_Log_Models_GetHistogramsRequest($project, $logstore, $from, $to, $topic, '');
    
    try {
        $response = $client->getHistograms($request);
        var_dump($response);
    } catch (Aliyun_Log_Exception $ex) {
        var_dump($ex);
    } catch (Exception $ex) {
        var_dump($ex);
    }
}
function listShard(Aliyun_Log_Client $client,$project,$logstore){
    $request = new Aliyun_Log_Models_ListShardsRequest($project,$logstore);
    try
    {
        $response = $client -> listShards($request);
        var_dump($response);
    } catch (Aliyun_Log_Exception $ex) {
        var_dump($ex);
    } catch (Exception $ex) {
        var_dump($ex);
    }
}
function batchGetLogs(Aliyun_Log_Client $client,$project,$logstore)
{
    $listShardRequest = new Aliyun_Log_Models_ListShardsRequest($project,$logstore);
    $listShardResponse = $client -> listShards($listShardRequest);
    foreach($listShardResponse-> getShardIds()  as $shardId)
    {
        $getCursorRequest = new Aliyun_Log_Models_GetCursorRequest($project,$logstore,$shardId,null, time() - 60);
        $response = $client -> getCursor($getCursorRequest);
        $cursor = $response-> getCursor();
        $count = 100;
        while(true)
        {
            $batchGetDataRequest = new Aliyun_Log_Models_BatchGetLogsRequest($project,$logstore,$shardId,$count,$cursor);
            var_dump($batchGetDataRequest);
            $response = $client -> batchGetLogs($batchGetDataRequest);
            if($cursor == $response -> getNextCursor())
            {
                break;
            }
            $logGroupList = $response -> getLogGroupList();
            foreach($logGroupList as $logGroup)
            {
                print ($logGroup->getCategory());

                foreach($logGroup -> getLogsArray() as $log)
                {
                    foreach($log -> getContentsArray() as $content)
                    {
                        print($content-> getKey().":".$content->getValue()."\t");
                    }
                    print("\n");
                }
            }
            $cursor = $response -> getNextCursor();
        }
    }
}
function deleteShard(Aliyun_Log_Client $client,$project,$logstore,$shardId)
{
    $request = new Aliyun_Log_Models_DeleteShardRequest($project,$logstore,$shardId);
    try
    {
        $response = $client -> deleteShard($request);
        var_dump($response);
    }catch (Aliyun_Log_Exception $ex) {
        var_dump($ex);
    } catch (Exception $ex) {
        var_dump($ex);
    }
}
function mergeShard(Aliyun_Log_Client $client,$project,$logstore,$shardId)
{
    $request = new Aliyun_Log_Models_MergeShardsRequest($project,$logstore,$shardId);
    try
    {
        $response = $client -> mergeShards($request);
        var_dump($response);
    }catch (Aliyun_Log_Exception $ex) {
        var_dump($ex);
    } catch (Exception $ex) {
        var_dump($ex);
    }
}
function splitShard(Aliyun_Log_Client $client,$project,$logstore,$shardId,$midHash)
{
    $request = new Aliyun_Log_Models_SplitShardRequest($project,$logstore,$shardId,$midHash);
    try
    {
        $response = $client -> splitShard($request);
        var_dump($response);
    }catch (Aliyun_Log_Exception $ex) {
        var_dump($ex);
    } catch (Exception $ex) {
        var_dump($ex);
    }
}
$endpoint = 'cn-shenzhen.log.aliyuncs.com';
$accessKeyId = 'LTAI4G3NncgK1zMevFQQdkWu';
$accessKey = 'ZyJwS8vX0tO8YsgwmbGg3Cn98lhHSf';
$project = 'fitdays-cn-test-log';
$logstore = 'fitdays-cn-test-logstore';
$token = "";

$client = new Aliyun_Log_Client($endpoint, $accessKeyId, $accessKey,$token);
//dd($client);
/*listShard($client,$project,$logstore);
mergeShard($client,$project,$logstore,82);
deleteShard($client,$project,$logstore,21);
splitShard($client,$project,$logstore,84,"0e000000000000000000000000000000");*/
putLogs($client, $project, $logstore);
/*listShard($client,$project,$logstore);
batchGetLogs($client,$project,$logstore);
listLogstores($client, $project);
listTopics($client, $project, $logstore);
getHistograms($client, $project, $logstore);
getLogs($client, $project, $logstore);*/
